import NetResp from "./NetResp";
import QuickTool from "../Tool/QuickTool";

export default class HttpTool {
    baseUrl: string = "http://121.239.169.91:9255/"

    public static s_instance : HttpTool = null;
    public static getInstance() : HttpTool {
        if (this.s_instance == null){
            this.s_instance = new HttpTool()
        }

        return this.s_instance;
    }

    static  s_uploadData(param: any) {
        this.getInstance().post("api/9255/abiGameTraining/saveGameRecord", param, null)
    }

    static s_get (action : string, param : any, cb : any) { this.getInstance().get(action, param, cb) }

    get(action : string, param : any, cb : any) {
        let finalUrl = this.baseUrl + action

        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = ()=> {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var response = xhr.responseText;
                if (!QuickTool.isNullToo(cb)){
                    response = JSON.parse(response)
                    QuickTool.safeCall1(cb, this.bulidResp(null, response))
                }
            }
        }

        let queryStrings: string[] = [];
        for (let q in param) {
            queryStrings.push(q +'=' + param[q]);
        }
        let queryString : string = queryStrings.join('&');
        finalUrl += ('?' + queryString);
        xhr.open("GET", finalUrl, true)
        xhr.send();
    }

    static s_post (action : string, param : any, cb : any) { this.getInstance().post(action, param, cb) }

    post (action : string, param : any, cb : any) {//url action success param
        console.log('post =============')
        console.log(param)
        let finalUrl = this.baseUrl + action

        // if (!QuickTool.isNullToo(param)){
        //     let keys = Object.keys(param)
        //     finalUrl += "?"
        //     for (let i=0;i<keys.length;i++){
        //         if (i!=0) finalUrl += "&"
        //         finalUrl += (keys[i]+"="+param[keys[i]])
        //     }
        // }
        // cc.log(finalUrl)

        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = ()=>{
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var response = xhr.responseText;
                if (!QuickTool.isNullToo(cb)){
                    response = JSON.parse(response)
                    QuickTool.safeCall1(cb, this.bulidResp(null, response))
                }
            }
        };

        xhr.open("POST", finalUrl, true);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        // xhr.setRequestHeader('Access-Control-Allow-Origin', 'https://ds.haoping.la/');

        if (!QuickTool.isNullToo(param)){
            xhr.send(this.encodeFormData(param));
        }else{
            xhr.send();
        }
    }

    //将对象{find:"pizza",zipcode:01234,radius:"1km"}变成表单编码的格式 find=pizza&zipcode=01234$radius=1km 的形式
    encodeFormData(data){
        if(!data) return "";
        var pairs=[];
        for(var name in data) {
            if(!data.hasOwnProperty(name)) continue; //跳过继承方法
            if(typeof  data[name] === "function") continue;//跳过方法
            var value=data[name].toString();
            name=encodeURIComponent(name.replace("%20","+"));
            value=encodeURIComponent(value.replace("%20","+"));
            pairs.push(name+"="+value);
        }
        return pairs.join('&');
    }

    bulidResp(netErr : Error, response) : NetResp {
        let resp = new NetResp()

        if (netErr == null) {
            resp.data = response
        }else{
            resp.netError = true
        }

        return resp
    }
}

/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */
